from django.contrib import admin

# Register your models here.
from django.contrib import admin
from .models import Sku, skuAndAttribute, attribute

# 新增内联管理类
class skuAndAttributeInline(admin.TabularInline):
    model = skuAndAttribute
    extra = 0  # 默认显示1个空白表单
    autocomplete_fields = ['attribute']  # 添加属性搜索功能

class SkuInline(admin.TabularInline):
    model = Sku
    extra = 0
    autocomplete_fields = ['attribute']

class SkuAdmin(admin.ModelAdmin):
    inlines = [skuAndAttributeInline]  # 添加内联
    list_display = ( 'product','price', 'stock', 'display_attributes')
    search_fields = ('product__name',)

    # 添加自定义字段显示
    def display_attributes(self, obj):
        return ", ".join([str(a.attribute) for a in obj.skuandattribute_set.all()])
    display_attributes.short_description = '关联属性'

class AttributeAdmin(admin.ModelAdmin):
    list_display = ('name', 'value')
    search_fields = ('name', 'value')

admin.site.register(Sku, SkuAdmin)
admin.site.register(attribute, AttributeAdmin)
